package com.lagou.study;

import com.lagou.study.bean.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class AnnotationConsumerMain {

    private static TreeMap<Long, Long> methodAMap = new TreeMap<>(Comparator.naturalOrder());
    private static TreeMap<Long, Long> methodBMap = new TreeMap<>(Comparator.naturalOrder());
    private static TreeMap<Long, Long> methodCMap = new TreeMap<>(Comparator.naturalOrder());

    public static void main(String[] args) throws IOException, InterruptedException {
        //ExecutorService cacheExecutor = Executors.newCachedThreadPool();
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);

        Calendar calendar = Calendar.getInstance();
        Date firstTime = calendar.getTime();
        // 间隔：5秒
        long period = 5000;

        Timer timer = new Timer();


        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // 执行你的方法
                Long TP90A = 0L;
                Long TP90B = 0L;
                Long TP90C = 0L;
                Long TP99A = 0L;
                Long TP99B = 0L;
                Long TP99C = 0L;
                for (Map.Entry entry:methodAMap.entrySet()) {
                    Long value = (Long) entry.getValue();
                    if(System.currentTimeMillis() - value >60000){
                        methodAMap.remove(entry.getKey());
                    }
                    int total = methodAMap.size();
                    Double tp90 = Math.ceil((double)total*90/100);
                    Double tp99 = Math.ceil((double)total*99/100);
                    Map.Entry tp90Entry = methodAMap.floorEntry(tp90.longValue());
                    Map.Entry tp99Entry = methodAMap.floorEntry(tp99.longValue());
                    TP90A = (Long) tp90Entry.getKey();
                    TP99A = (Long) tp99Entry.getKey();
                }
                for (Map.Entry entry:methodBMap.entrySet()) {
                    Long value = (Long) entry.getValue();
                    if(System.currentTimeMillis() - value >60000){
                        methodBMap.remove(entry.getKey());
                    }
                    int total = methodBMap.size();
                    Double tp90 = Math.ceil((double)total*90/100);
                    Double tp99 = Math.ceil((double)total*99/100);
                    Map.Entry tp90Entry = methodBMap.floorEntry(tp90.longValue());
                    Map.Entry tp99Entry = methodBMap.floorEntry(tp99.longValue());
                    TP90B = (Long) tp90Entry.getKey();
                    TP99B = (Long) tp99Entry.getKey();
                }
                for (Map.Entry entry:methodCMap.entrySet()) {
                    Long value = (Long) entry.getValue();
                    if(System.currentTimeMillis() - value >60000){
                        methodCMap.remove(entry.getKey());
                    }
                    int total = methodCMap.size();
                    Double tp90 = Math.ceil((double)total*90/100);
                    Double tp99 = Math.ceil((double)total*99/100);
                    Map.Entry tp90Entry = methodCMap.floorEntry(tp90.longValue());
                    Map.Entry tp99Entry = methodCMap.floorEntry(tp99.longValue());
                    TP90C = (Long) tp90Entry.getKey();
                    TP99C = (Long) tp99Entry.getKey();
                }
                System.out.println("methodA TP90:" + TP90A + "毫秒");
                System.out.println("methodB TP90:" + TP90B + "毫秒");
                System.out.println("methodC TP90:" + TP90C + "毫秒");
                System.out.println("methodA TP99:" + TP99A + "毫秒");
                System.out.println("methodB TP99:" + TP99B + "毫秒");
                System.out.println("methodC TP99:" + TP99C + "毫秒");
            }
        }, firstTime, period);



        while (true) {
            for (int i = 0; i<1000 ; i++) {
                Thread.sleep(10);
                new Thread(() -> {
                    service.methodA();
                    if("methodA".equals(Constant.methodName.get())){
                        methodAMap.put(Constant.time.get(),System.currentTimeMillis());
                    }
                    //methodAMap.put(Long.valueOf(RpcContext.getContext().getAttachment("methodA")),System.currentTimeMillis());
                }).start();
                new Thread(() -> {
                    service.methodB();
                    if("methodB".equals(Constant.methodName.get())){
                        methodBMap.put(Constant.time.get(),System.currentTimeMillis());
                    }
                    //methodBMap.put(Long.valueOf(RpcContext.getContext().getAttachment("methodB")),System.currentTimeMillis());
                }).start();
                new Thread(() -> {
                    service.methodC();
                    if("methodC".equals(Constant.methodName.get())){
                        methodCMap.put(Constant.time.get(),System.currentTimeMillis());
                    }
                    //methodCMap.put(Long.valueOf(RpcContext.getContext().getAttachment("methodC")),System.currentTimeMillis());
                }).start();
            }
        }
    }

    @Configuration
    @EnableDubbo(scanBasePackages = "com.lagou.study.bean")
    @PropertySource("classpath:/dubbo-provider.properties")
    @ComponentScan("com.lagou.study.bean")
    static class ConsumerConfiguration {

    }
}
